package com.cm.etax.monitor;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;

/**
 * 任务监视器
 *
 * @author LWang 2023.02.03
 * @since 1.0.0
 */
public class Monitor extends JFrame {
    // 内部组件
    /**
     * 运行实例 ID Label
     */
    private JLabel instanceLabel;

    /**
     * 用户 ID Label
     */
    private JLabel userLabel;

    /**
     * 任务 ID Label
     */
    private JLabel taskLabel;

    /**
     * 日志 label
     */
    private JLabel logLabel;

    /**
     * 关闭按钮
     */
    private JButton closeButton;

    private final int width = 960;
    private final int height = 64;

    /**
     * 任务监视器启动服务
     */
    public Monitor() {
        JFrame.setDefaultLookAndFeelDecorated(false);
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setPreferredSize(new Dimension(width, height));
        this.setUndecorated(true);
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        this.setLocation(0, 500);
        this.setAlwaysOnTop(true);
        this.addComponent();
        this.pack();
        this.setVisible(true);
    }

    private final String instanceLabelTextPatter = "当前实例编号：%s";
    private final String userLabelTextPatter = "当前任务用户：%s";
    private final String taskLabelTextPatter = "当前任务 ID：%s";
    private final String logLabelTextPatter = "任务日志：%s";

    private void addComponent() {
        final var startX = 10;
        final var startY = 5;
        final var padding = 5;
        final var font = new Font("Microsoft YaHei UI", Font.PLAIN, 12);
        // 使用绝对定位布局
        this.setBounds(0, 0, width, height);
        var x = startX;
        var y = startY;
        this.getContentPane().setBackground(Color.getHSBColor(0.5000f, 0.1667f, 0.4706f));
        // 第一行：
        this.instanceLabel = new JLabel(instanceLabelTextPatter.formatted(StringUtils.EMPTY));
        this.instanceLabel.setBounds(x, y, 320, 22);
        this.instanceLabel.setForeground(Color.WHITE);
        this.instanceLabel.setFont(font);
        this.getContentPane().add(this.instanceLabel);

        x += this.instanceLabel.getWidth() + padding;
        this.userLabel = new JLabel(userLabelTextPatter.formatted(StringUtils.EMPTY));
        this.userLabel.setBounds(x, y, 240, 22);
        this.userLabel.setForeground(Color.WHITE);
        this.userLabel.setFont(font);
        this.getContentPane().add(this.userLabel);

        x += this.userLabel.getWidth() + padding;
        this.taskLabel = new JLabel(taskLabelTextPatter.formatted(StringUtils.EMPTY));
        this.taskLabel.setBounds(x, y, 240, 22);
        this.taskLabel.setForeground(Color.WHITE);
        this.taskLabel.setFont(font);
        this.getContentPane().add(this.taskLabel);

        x += this.taskLabel.getWidth() + padding * 2;
        this.closeButton = new JButton("关闭服务");
        this.closeButton.setBounds(x, y, 120, 54);
        this.closeButton.setFont(font);
        this.closeButton.setBackground(Color.getHSBColor(0.5986f, 0.5506f, 0.3490f));
        this.closeButton.setForeground(Color.WHITE);
        this.closeButton.setDefaultCapable(false);
        this.closeButton.setBorderPainted(false);
        this.closeButton.setFocusPainted(false);
        this.closeButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.closeButton.addActionListener(e -> exit());
        this.getContentPane().add(this.closeButton);
        x = startX;
        y += 32;
        this.logLabel = new JLabel(logLabelTextPatter.formatted(StringUtils.EMPTY));
        this.logLabel.setBounds(x, y, 810, 22);
        this.logLabel.setForeground(Color.WHITE);
        this.logLabel.setFont(font);
        this.getContentPane().add(this.logLabel);
    }

    private boolean doNext = true;

    public boolean doNext() {
        return this.doNext;
    }

    /**
     * 退出处理
     */
    public void exit() {
        this.doNext = false;
        this.dispose();
    }

    /**
     * @param userId 用户 ID
     */
    public void setUserId(String userId) {
        userLabel.setText(userLabelTextPatter.formatted(userId));
    }

    /**
     * @param taskId 任务 ID
     */
    public void setTaskId(String taskId) {
        taskLabel.setText(taskLabelTextPatter.formatted(taskId));
    }

    /**
     * 设置日志
     *
     * @param log 日志信息
     */
    public void setLog(String log) {
        logLabel.setText(logLabelTextPatter.formatted(log));
    }

    public static void main(String[] args) throws InterruptedException {
        var monitor = new Monitor();
        while (monitor.doNext()) {
            monitor.setTaskId(String.valueOf(RandomUtils.nextLong(0, 10000)));
            monitor.setUserId(RandomStringUtils.random(24, "甲乙丙丁戊己庚辛壬癸子丑寅卯辰巳无为申酉戌亥"));
            monitor.setLog(RandomStringUtils.random(100, "甲乙丙丁戊己庚辛壬癸子丑寅卯辰巳无为申酉戌亥"));
            TimeUnit.SECONDS.sleep(2);
        }
        System.exit(0);
    }
}
